package com.example.demo.bizhan.thread;

/**
 * @description: xxx
 * @time: 2020-10-28 21:25
 * @author: baojinlong
 **/
public class ProducerConsumerDemo {
    public static void main(String[] args) {
        Person person = new Person();
        new Thread(new Producer(person)).start();
        new Thread(new Consumer(person)).start();
        // 如果是多个那么person里面就不能用if而是用while
        new Thread(new Producer(person)).start();
        new Thread(new Consumer(person)).start();
    }
}


class Producer implements Runnable {
    private Person person;

    public Producer(Person person) {
        this.person = person;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                this.person.set("小队长", "男");
            } else {
                this.person.set("呼呼", "女");
            }
        }
    }
}

class Consumer implements Runnable {
    private Person person;

    public Consumer(Person person) {
        this.person = person;
    }

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            this.person.get();
        }
    }
}